<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
</head>
  <title>部门管理</title>
  <style type="text/css">
  	.layui-form-select dl{
		max-height: 150px; // 原值为300，调节高度建议为50的倍数
	}
	.layui-form-label{width: 130px;}
  </style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <!-- 查询区域  -->
      <div id="query-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
        	<div class="layui-inline">
            	<label class="layui-form-label">公司名称</label>
            	<div class="layui-input-inline">
	              	<select name="orgId" id="query-select-orgId" lay-filter="query_select_orgId" lay-search>
	              		<c:if test="${not empty orgInfos }">
	              			<c:forEach items="${orgInfos}" var="org">
	              				<option value="${org.id }" <c:if test="${org.id == bindOrgId }">selected</c:if>>${org.compName }</option>
		            		</c:forEach>
	              		</c:if>
	              	</select>
            	</div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
              <input type="text" name="deptName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
			<div class="layui-inline">
				<label class="layui-form-label">日期</label>
				<div class="layui-input-inline">
					<input type="text" class="layui-input" id="dept-laydate-dates" name="dates" readonly
						   placeholder="请选择创建时间">
				</div>
			</div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
              <infopub:select id="query-form-state" entireName="-状态" name="state" type="dic" groupNo="A00010020"></infopub:select>
            </div>
          </div>
          <shiro:hasPermission name="LIST_DEPT">
	          <div class="layui-inline">
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-dept-search">
	              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
	            </button>
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-reset">
	              <i class="layui-icon layui-icon-fonts-clear  layuiadmin-button-btn"></i>重置
	            </button>
	          </div>
          </shiro:hasPermission>
        </div>
      </div>
  	  <!-- 按钮区域  -->
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <shiro:hasPermission name="SAVE_DEPT">
          	<button class="layui-btn layuiadmin-btn-admin " data-type="add">
          	 	<i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新增
          	</button>
          </shiro:hasPermission>
        </div>
        <!-- 数据表单 -->
        <table id="LAY-dept-manage" lay-filter="LAY-dept-manage"></table>

        <div class="layui-form" lay-filter="add_form" id="add_form" style="padding: 20px 30px 0 0;display:none">
		    <div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">公司名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="orgName" id="add_orgName" style="border:0px;width:220px;" readonly="readonly" class="layui-input">
			        	<input type="hidden" name="orgId" id="add_orgId" value="${bindOrgId }" />
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		      	<div class="layui-inline">
			    	<label class="layui-form-label">上级部门（选填）</label>
				    <div class="layui-input-inline">
						<select name="parentId" id="add_selectParentDept" lay-filter="add_selectParentDept" lay-search>
						</select>
				    </div>
			    </div>
			</div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门层级</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="levelIdx" id="add_levelIdx" value="1" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="deptName" maxlength="50" lay-verify="required" placeholder="请输入部门名称" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">编制人数</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptMaxNum" maxlength="5" lay-verify="required|intNumber" placeholder="请输入编制人数" autocomplete="off" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">部门负责人</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptLeader" maxlength="10" lay-verify="required" placeholder="请输入部门负责人" autocomplete="off" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      	<input type="button" lay-submit lay-filter="add_form_submit" id="add_form_submit" value="确认">
		    </div>
	  	</div>

	  	<!-- 编辑窗口 -->
        <div class="layui-form" lay-filter="edit_form" id="edit_form" style="padding: 20px 30px 0 0;display:none">
        	<input type="hidden" name="id" id="editAccountId" />
		    <div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">公司名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="orgName" id="add_orgName" style="border:0px;width:220px;" readonly="readonly" class="layui-input">
			        	<input type="hidden" name="orgId" id="add_orgId" />
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		      	<div class="layui-inline">
			    	<label class="layui-form-label">上级部门（选填）</label>
				    <div class="layui-input-inline">
						<select name="parentId" id="edit_selectParentDept" lay-filter="edit_selectParentDept" lay-search>
						</select>
				    </div>
			    </div>
			</div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门层级</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="levelIdx" id="edit_levelIdx" value="1" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="deptName" maxlength="50" lay-verify="required" placeholder="请输入部门名称" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">编制人数</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptMaxNum" maxlength="5" lay-verify="required|intNumber" placeholder="请输入编制人数" autocomplete="off" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">部门负责人</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptLeader" maxlength="10" lay-verify="required" placeholder="请输入部门负责人" autocomplete="off" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="edit_form_submit" id="edit_form_submit" value="确认">
		    </div>
		    <!-- 岗位明细table -->
        	<table id="LAY-post-detail" lay-filter="LAY-post-detail"></table>
	  	</div>

	  	<!-- 新增岗位window -->
	  	<div class="layui-form" lay-filter="add_detail_from" id="add_detail_from" style="padding: 20px 30px 0 0;display:none">
	  		<input type="hidden" name="orgId"/>
	  		<input type="hidden" name="deptId"/>
		    <div class="layui-form-item">
		      	<label class="layui-form-label">岗位名称</label>
		      	<div class="layui-input-inline">
			      	<input type="text" name="postName" maxlength="30" lay-verify="required" placeholder="请输入岗位名称" autocomplete="off" class="layui-input">
		      	</div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="add_detail_from_submit" id="add_detail_from_submit" value="确认">
		    </div>
	  	</div>

	  	<!-- 编辑明细window -->
	  	<div class="layui-form" lay-filter="edit_detail_from" id="edit_detail_from" style="padding: 20px 30px 0 0;display:none">
	  		<input type="hidden" name="id" />
	  		<input type="hidden" name="orgId"/>
	  		<input type="hidden" name="deptId"/>
		    <div class="layui-form-item">
		      	<label class="layui-form-label">岗位名称</label>
		      	<div class="layui-input-inline">
			      	<input type="text" name="postName" maxlength="30" lay-verify="required" placeholder="请输入岗位名称" autocomplete="off" class="layui-input">
		      	</div>
		    </div>
		    <div class="layui-form-item">
		    	<label class="layui-form-label">岗位状态</label>
		    	<div class="layui-input-inline">
		    		<infopub:select entireName="-状态" name="state" layVerify="required" type="dic" groupNo="A00010020"></infopub:select>
		    	</div>
		  	</div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="edit_detail_from_submit" id="edit_detail_from_submit" value="确认">
		    </div>
	  	</div>

	  	<!-- 详情窗口 -->
        <div class="layui-form" lay-filter="detail_form" id="detail_form" style="padding: 20px 30px 0 0;display:none">
		    <div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">公司名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="orgName" style="border:0px;width:220px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		      	<div class="layui-inline">
			    	<label class="layui-form-label">上级部门（选填）</label>
				    <div class="layui-input-inline">
			        	<input type="text" name="parentDeptName" style="border:0px;" readonly="readonly" class="layui-input">
				    </div>
			    </div>
			</div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门层级</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="levelIdx" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">部门名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="deptName" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">编制人数</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptMaxNum" style="border:0px;" readonly="readonly" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
		    		<label class="layui-form-label">部门负责人</label>
		      		<div class="layui-input-inline">
		        		<input type="text" name="deptLeader" style="border:0px;" readonly="readonly" class="layui-input">
		      		</div>
			    </div>
		    </div>
		    <div class="layui-form-item">
			    <div class="layui-inline">
			    	<label class="layui-form-label">部门岗位</label>
			    	<div class="layui-input-inline">
				   		<textarea id="detail_postName" name="postName" readonly="readonly" class="layui-textarea" style="width:350px;height:90px;"></textarea>
			    	</div>
		    	</div>
		  	</div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="detail_form_submit" id="detail_form_submit" value="确认">
		    </div>
	  	</div>

      </div>
    </div>
  </div>
	<shiro:hasPermission name="ADD_POST_DEPT">
		<script type="text/html" id="table-toolbar-addPost">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addPost">
				<i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新增岗位
	        </button>
       	</div>
  	</script>
  	</shiro:hasPermission>
  <script>
  	layui.config({
    	base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  	}).extend({
    	index: 'lib/index' //主入口模块
  	}).use(['index', 'table', 'set', 'transfer', 'laydate'], function(){
    	var $ = layui.$
    	,form = layui.form
    	,table = layui.table
    	,laydate = layui.laydate;

		//日期
		laydate.render({
			elem: '#dept-laydate-dates'
			, range: '~'
		});

  	    // //开始日期
  	    // var insStart = laydate.render({
  	    //   elem: '#sent-laydate-start'
  	    //   ,done: function(value, date){
  	    //     //更新结束日期的最小日期
  	    //     insEnd.config.min = lay.extend({}, date, {
  	    //       month: date.month - 1
  	    //     });
		  //
  	    //     //自动弹出结束日期的选择器
  	    //    insEnd.config.elem[0].focus();
  	    //   }
  	    // });
		  //
  	    // //结束日期
  	    // var insEnd = laydate.render({
  	    //   elem: '#sent-laydate-end'
  	    //   ,done: function(value, date){
  	    //     //更新开始日期的最大日期
  	    //     insStart.config.max = lay.extend({}, date, {
  	    //       month: date.month - 1
  	    //     });
  	    //   }
  	    // });
 		// 正则校验
    	form.verify({
    		//验证汉字
    	    CHS: [
    	    	/^[\u0391-\uFFE5]+$/
    	    	,'只能输入汉字'
    	  	],
	    	//大于0的正整数
		  	intNumber:[
				/^[1-9]\d*$/
				,'请输入正确的数字'
		  	]
		});

    	// 表单
    	table.render({
      		elem: '#LAY-dept-manage'
      		,url: basePath + '/dept/queryDataByCondition' //请求数据接口
      		,cols: [[
        		{type: 'checkbox', fixed: 'left'}
        		,{field: 'deptCode', title: '部门ID',width:100}
        		,{field: 'orgName', title: '公司名称',width:280}
        		,{field: 'deptName', title: '部门名称',width:140}
        		,{field: 'levelIdx', title: '部门层级',width:90}
        		,{field: 'parentDeptName', title: '上级部门',width:140, templet: function(d){
	    				return d.parentDeptName||global_null_tip;
	          		}
        		}
        		,{field: 'deptLeader', title: '部门负责人',width:130, templet: function(d){
	  	      		  return d.deptLeader||global_null_tip;
	  	        	}
        		}
        		,{field: 'addTime', title: '创建时间',width:170, templet: function(d){
            		  	return layui.util.toDateString(d.addTime,"yyyy-MM-dd HH:mm:ss");
	              	}
	             }
        		,{field: 'state', title: '状态', fixed: 'right',width:80, templet: function (d) {
	        			if(d.state==1){
	          				return "启用中";
	          			}else{return '<span class="layui-badge layui-bg-cyan">已停用</span>' ;}
		        	}
 		    	}
        		,{title: '操作', width: 220, align:'center', fixed: 'right', templet: function(d){
      	  				var optStr ="";
      	  				<shiro:hasPermission name="VIEW_DEPT">
      	  					optStr += '<a class="layui-btn layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-form"></i>查看</a>';
  	  	  				</shiro:hasPermission>
      	  				<shiro:hasPermission name="SAVE_DEPT">
      	 					optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
  	  	  				</shiro:hasPermission>
  	  	  				<shiro:hasPermission name="UPDATE_STATUS_DEPT">
		  	  	  			if(d.state == 0){//状态为停用
      	 						optStr += '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="use"><i class="layui-icon layui-icon-ok"></i>启用</a>';
		   	        		}else if(d.state == 1){//状态为启用
      	 						optStr += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="freeze"><i class="layui-icon layui-icon-pause"></i>停用</a>';
		   	        		}
						</shiro:hasPermission>
			  		  	return optStr;
        			}
        		}
      			]]
      		,page: true
      		,limit: 10
    	});

    	<shiro:hasPermission name="LIST_DEPT">
	    	//监听搜索
	    	form.on('submit(LAY-dept-search)', function(data){
		      	//执行重载
		      	table.reload('LAY-dept-manage', {page: {curr: 1},
		        	where: trimData(data)
		      	});
	    	});
	    	//重置清空
		    form.on('submit(LAY-user-back-reset)', function(data){
		    	reset_form('query-form');
		    	//额外处理select
				$("#query-form-state").val('');
				if('${bindOrgId}'==null||'${bindOrgId}'==""){
					$("#query-select-orgId").val('${superOrgId}');
				}else{
					$("#query-select-orgId").val('${bindOrgId}');
				}
				form.render();
		    });
    	</shiro:hasPermission>

    	// 监听操作列
    	table.on('tool(LAY-dept-manage)', function(obj){
      		var data = obj.data;
      		if(obj.event === 'use'){
          		layer.confirm('确定启用？',{icon:3, title :'提示'}, function(index){
          			$.ajax({
  						type : 'post',
  						url : basePath + '/dept/updateStatus?id=' + data.id,
  						success:function(result){
  							if(result.success){
  								table.reload('LAY-dept-manage'); //数据刷新
  								layer.close(index);
  							}else{
  								layer.msg(result.msg,{icon: 7});
  				  			}
  						}
  					});
          		});
      		}else if(obj.event === 'freeze'){
          		layer.confirm('确定停用？',{icon:3, title :'提示'}, function(index){
          			$.ajax({
  						type : 'post',
  						url : basePath + '/dept/updateStatus?id=' + data.id,
  						success:function(result){
  							if(result.success){
  								table.reload('LAY-dept-manage'); //数据刷新
  								layer.close(index);
  							}else{
  								layer.msg(result.msg,{icon: 7});
  				  			}
  						}
  					});
          		});
      		} else if(obj.event === 'edit'){
      			$('#edit_selectParentDept').html('');
      			//获取一级二级部门，形成上级部门下拉选项
      			$.ajax({
      				type : "POST", //提交方式
      				url : "${ctx}/dept/getDeptByOrgId",//路径
      				data : {
      					"orgId" :data.orgId
      				},//数据，这里使用的是Json格式进行传输
      				success : function(result) {//返回数据根据结果进行相应的处理
      					if(result.flag==true){
      						var strBuffer = "";
      						strBuffer+= '<option value="">请选择</option>';
      						layui.$.each(result.depts, function(i,item){
      							if(item.id != null && item.deptName != null){
      								if(item.id != data.id){//不可以选自己为上级部门
	      								strBuffer+= '<option data='+item.levelIdx+' value='+item.id+' ';
	      								if(item.id == data.parentId){
	      									strBuffer+= 'selected';
	      								}
	      								strBuffer+= ' >'+item.deptName+'</option>';
      								}
      							}
      				        });
      						$('#edit_selectParentDept').append(strBuffer);
      						form.render();
      					}else{
      						layer.msg('数据通信异常~',{icon: 0});
      					}
      				}
      			});
      			form.val('edit_form',data); // 填充原值
      			$('input[name=orgId]').val(data.orgId);
      			$('input[name=deptId]').val(data.id);
      			//岗位table渲染
      	        table.render({
      	          elem: '#LAY-post-detail'
      	          ,url: basePath + '/post/queryDataByCondition?deptId='+data.id //请求数据接口
      	          ,toolbar:'#table-toolbar-addPost'
      	          ,defaultToolbar: []
      	          ,cols: [[
					{type: 'checkbox', fixed: 'left'}
      	            ,{field: 'postCode', title: '岗位编号'}
      	            ,{field: 'postName', title: '岗位名称',width:180}
      	            ,{field: 'state', title: '岗位状态', templet: function(d){
	      	            	if(d.state == 1){
	          					return "启用中";
	          				}else{
	          					return '<span class="layui-badge layui-bg-cyan">已停用</span>' ;
          					}
          				}
      	            }
      	            ,{title: '操作', width: 110, align:'center', fixed: 'right', templet: function(d){
      	    	      	  	var optStr ="";
      	    	  	  	  	<shiro:hasPermission name="UPDATE_POST_DEPT">
      	    	  	    		 optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="editPost"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
      	    	  		  	</shiro:hasPermission>
      	    	  		  	return optStr;
      	            	}
      	            }
      	          ]]
      	          ,page: true
      	          ,limit: 5
      	          ,limits:[5,10,20,30,50]
      	        });
      			layer.open({
            		type: 1
            		,title: '编辑部门'
            		,content: $('#edit_form')
            		,area: layui.admin.screen() < 2 ? ['80%', '550px'] : ['720px','550px']
            		,btn: ['确定', '取消']
            		,btnAlign: 'c'
            		,yes: function(index, layero){
          	  			var submitID = 'edit_form_submit'
          		  		,submit = layero.contents().find('#'+ submitID);
          	  			//监听提交
          	  			form.on('submit('+ submitID +')', function(data){
                			var field = data.field; //获取提交的字段
                			//提交 Ajax 成功后，静态更新表格中的数据
                			$.ajax({
                    			type : 'post',
  								url : basePath + '/dept/save',
                    			data: field,
  								success:function(result){
  									if(result.success){
                						table.reload('LAY-dept-manage'); //数据刷新
                						layer.close(index); //关闭弹层
  									}else{
  										layer.msg(result.msg,{icon: 7});
  					  				}
  								}
                			});
              			});
              		submit.trigger('click');
           		}
          	});
       	} else if(obj.event === 'view'){
  			form.val('detail_form',data); // 填充原值
  			if(data.postInfos.length > 0){
  				var postName = '';
  				for(var i in data.postInfos){
  					var postInfo = data.postInfos[i];
  					postName = postName + postInfo.postName + "；";
  				}
  				$("#detail_postName").val(postName);
  			}
  			layer.open({
        		type: 1
        		,title: '部门详情'
        		,content: $('#detail_form')
        		,area: layui.admin.screen() < 2 ? ['80%', '520px'] : ['620px','520px']
      		});
   		}
    	});

     	// 岗位table监听toolbar
        table.on('toolbar(LAY-post-detail)', function(obj){
          	var data = obj.data;
          	if(obj.event === 'addPost'){
          		layer.open({
    	        	  type: 1
    	        	  ,title: '添加岗位'
    	        	  ,content: $('#add_detail_from')
    	        	  ,area: layui.admin.screen() < 2 ? ['80%', '210px'] : ['450px','210px']
    	        	  ,btn: ['确定', '取消']
    	        	  ,btnAlign: 'c'
    	        	  ,yes: function(index, layero){
    	        		  var submitID = 'add_detail_from_submit'
    	            		,submit = layero.contents().find('#'+ submitID);
    	        		  //监听提交
    	        		  form.on('submit('+ submitID +')', function(data){
    	        			  var field = data.field; //获取提交的字段
    	        			  //提交 Ajax 成功后，静态更新表格中的数据
    	        			  $.ajax({
    	        				  type : 'post',
    	        				  url : basePath + '/post/save',
    	        				  data: field,
    	        				  success:function(result){
    	        					  if(result.success){
    	        						  table.reload('LAY-post-detail'); //数据刷新
    	        						  layer.close(index); //关闭弹层
    	        						  clean_form("add_detail_from");//清空表单
    	        					  }else{
    	        						  layer.msg(result.msg,{icon: 7});
    	        					  }
    	        				  }
    	              		 });
    	            	});
    	            submit.trigger('click');
    	          }
    	        });
          	}
        });
     	// 岗位table监听操作列
        table.on('tool(LAY-post-detail)', function(obj){
          var data = obj.data;
          if(obj.event === 'editPost'){
            	form.val('edit_detail_from',data); // 填充原值
            	layer.open({
    	        	  type: 1
    	        	  ,title: '编辑岗位'
    	        	  ,content: $('#edit_detail_from')
    	        	  ,area: layui.admin.screen() < 2 ? ['80%', '300px'] : ['420px','300px']
    	        	  ,btn: ['确定', '取消']
    	        	  ,btnAlign: 'c'
    	        	  ,yes: function(index, layero){
    	        		  var submitID = 'edit_detail_from_submit'
    	            		,submit = layero.contents().find('#'+ submitID);
    	        		  //监听提交
    	        		  form.on('submit('+ submitID +')', function(data){
    	        			  var field = data.field; //获取提交的字段
    	        			  //提交 Ajax 成功后，静态更新表格中的数据
    	        			  $.ajax({
    	        				  type : 'post',
    	        				  url : basePath + '/post/save',
    	        				  data: field,
    	        				  success:function(result){
    	        					  if(result.success){
    	        						  table.reload('LAY-post-detail'); //数据刷新
    	        						  layer.close(index); //关闭弹层
    	        						  clean_form("edit_detail_from");//清空表单
    	        					  }else{
    	        						  layer.msg(result.msg,{icon: 7});
    	        					  }
    	        				  }
    	              		 });
    	            	});
    	            submit.trigger('click');
    	          }
    	        });
            }
        });
    // 按钮事件
    var active = {
      add: function(){
   	    clean_form("add_form");//清空表单
   	 	var orgId = $("#query-select-orgId").val();
   	 	var orgName = $("#query-select-orgId").find("option:selected").text();
   	 	$('#add_orgId').val(orgId);
   	 	$('#add_orgName').val(orgName);
   	 	$("#add_levelIdx").val(1);
   	 	$('#add_selectParentDept').html('');
		//获取一级二级部门，形成上级部门下拉选项
		$.ajax({
			type : "POST", //提交方式
			url : "${ctx}/dept/getDeptByOrgId",//路径
			data : {
				"orgId" :orgId
			},//数据，这里使用的是Json格式进行传输
			success : function(result) {//返回数据根据结果进行相应的处理
				if(result.flag==true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择</option>';
					layui.$.each(result.depts, function(i,item){
						if(item.id != null && item.deptName != null){
							strBuffer+= '<option data='+item.levelIdx+' value='+item.id+'>'+item.deptName+'</option>';
						}
			        });
					$('#add_selectParentDept').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			}
		});
		form.render();
        layer.open({
          type: 1
          ,title: '添加部门'
          ,content: $('#add_form')
          ,area: layui.admin.screen() < 2 ? ['80%', '520px'] : ['520px','520px']
          ,btn: ['确定', '取消']
          ,btnAlign: 'c'
          ,yes: function(index, layero){
        	  var submitID = 'add_form_submit'
            	,submit = layero.contents().find('#'+ submitID);

              //监听提交
           	  form.on('submit('+ submitID +')', function(data){
              	var field = data.field; //获取提交的字段
              	//提交 Ajax 成功后，静态更新表格中的数据
              	$.ajax({
                  	type : 'post',
					url : basePath + '/dept/save',
                  	data: field,
					success:function(result){
						if(result.success){
              				table.reload('LAY-dept-manage'); //数据刷新
              				clean_form("add_form");//清空表单
              				layer.close(index); //关闭弹层
						}else{
							layer.msg(result.msg,{icon: 7});
					  	}
					}
              	});
            });
            submit.trigger('click');
          }
        });
      }
    }
    // data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  	//查询-公司select监听
    form.on('select(query_select_orgId)', function(data){
    	var orgId = data.value;
    	$('#add_orgId').val(orgId);
    	var orgName = data.elem[data.elem.selectedIndex].text;
		$('#add_orgName').val(orgName);
	});

  	//新增-上级部门select监听
    form.on('select(add_selectParentDept)', function(data){
    	if(data.value != ''){
	    	//获取上级部门的层级
	    	var parentLevelIdx = $("#add_selectParentDept").find("option:selected").attr("data");
	    	$("#add_levelIdx").val(parseInt(parentLevelIdx) + 1);
    	}else{
	    	$("#add_levelIdx").val(1);
    	}
	});

  	//编辑-上级部门select监听
    form.on('select(edit_selectParentDept)', function(data){
    	if(data.value != ''){
	    	//获取上级部门的层级
	    	var parentLevelIdx = $("#edit_selectParentDept").find("option:selected").attr("data");
	    	$("#edit_levelIdx").val(parseInt(parentLevelIdx) + 1);
    	}else{
	    	$("#edit_levelIdx").val(1);
    	}
	});
  });

	//添加验证
  	function verify(obj,verType){
		var $ = layui.$;
		if(obj.value != null && obj.value != ''){
			$(obj).attr("lay-verify",verType);
		}else{
			$(obj).removeAttr("lay-verify");
		}
	}
  </script>
</body>
</html>

